package com.cp.yn.activity

import android.content.Intent
import android.os.Bundle
import cn.bingoogolapple.qrcode.core.QRCodeView
import com.common.base.base.BaseActivity
import com.common.base.tools.arouter.endRouter
import com.cp.yn.databinding.ActivityScanBinding
import com.cp.yn.manager.RouterManager
import com.didi.drouter.annotation.Router

@Router(path = RouterManager.Path.APP_Scan)
class ScanActivity : BaseActivity<ActivityScanBinding>() {
    override fun viewBinding(): ActivityScanBinding {
        return ActivityScanBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initScan()
    }

    private fun initScan() {
        viewBinding.zxingView.apply {
            startSpot()
            setDelegate(object : QRCodeView.Delegate {
                //处理打开相机出错
                override fun onCameraAmbientBrightnessChanged(isDark: Boolean) {
                }

                //摄像头环境亮度发生变化
                //Params:
                //isDark – 是否变暗
                override fun onScanQRCodeOpenCameraError() {

                }

                //处理扫描结果
                //Params:
                //result – 摄像头扫码时只要回调了该方法 result 就一定有值，不会为 null。解析本地图片或 Bitmap 时 result 可能为 null
                override fun onScanQRCodeSuccess(result: String?) {
                    endRouter(Intent().putExtra(RouterManager.Key.APP_Scan_Result, result))
                }
            })
        }
    }
}